/**
 * @file:     leds.c
 * @Author:   ChenBlue
 * @email:    ChenBlue952@qq.com
 * @data:     Tue Jan 31 02:18:57 2023
 * @brief:
 */

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>

#define LEDOFF 0 /* 关灯 */
#define LEDON 1 /* 开灯 */

/* gpioled设备结构体 */
struct led_dev {
	dev_t devid; /* 设备号    */
	struct cdev cdev; /* cdev     */
	struct class* class; /* 类       */
	struct device* device; /* 设备      */
	int major; /* 主设备号   */
	int minor; /* 次设备号   */
	struct device_node* nd; /* 设备节点 */
	int gpio; /* led所使用的GPIO编号      */
};
struct led_dev led1; /* led设备 */

static int yc_leds_open(struct inode* inode, struct file* file)
{
	file->private_data = &led1; /* 设置私有数据 */

	return 0;
}
static ssize_t yc_leds_read(struct file* file, char __user* buff, size_t count, loff_t* offp)
{
	char value;
	int ret;
	struct led_dev* dev = file->private_data;

	value = gpio_get_value(dev->gpio);
	ret = copy_to_user(buff, &value, 1);
	if (ret < 0) {
		printk("copy_to_user failed!\r\n");
		return -EFAULT;
	}
	return count;
}

static ssize_t yc_leds_write(struct file* file, const char __user* buf, size_t count, loff_t* ppos)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;
	struct led_dev* dev = file->private_data;

	retvalue = copy_from_user(databuf, buf, count);
	if (retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}
	ledstat = databuf[0]; /* 获取状态值 */

	if (ledstat == LEDON) {
		gpio_set_value(dev->gpio, 1); /* 打开LED灯 */
	} else if (ledstat == LEDOFF) {
		gpio_set_value(dev->gpio, 0); /* 关闭LED灯 */
	}
	return 0;
}

static struct file_operations yc_leds_fops = {
	.owner = THIS_MODULE,
	.open = yc_leds_open,
	.read = yc_leds_read,
	.write = yc_leds_write,
};

static ssize_t yc_led_show(struct device* dev,
    struct device_attribute* attr, char* buf)
{
	return sprintf(buf, "%d\n", gpio_get_value(led1.gpio));
}

static ssize_t yc_led_store(struct device* dev,
    struct device_attribute* attr, const char* buf, size_t size)
{
	switch (buf[0]) {
	case '0':
		gpio_set_value(led1.gpio, 0);
		break;
	case '1':
		gpio_set_value(led1.gpio, 1);
		break;
	default:
		printk(KERN_INFO "%s: err paramater\n", __func__);
		break;
	}
	return size;
}

static struct device_attribute yc_led_on_off_attr = {
	.attr = {
	    .name = "led_on_off",
	    .mode = S_IRUSR | S_IWUSR,
	},
	.show = yc_led_show,
	.store = yc_led_store,
};

static int led_probe(struct platform_device* pdev)
{
	int ret;
	led1.gpio = of_get_named_gpio(pdev->dev.of_node->child, "gpios", 0);
	if (led1.gpio < 0) {
		printk("can't get led-gpios");
		return -1;
	}
	alloc_chrdev_region(&led1.devid, 0, 1, "yc-leds"); // cat /proc/devices
	led1.major = MAJOR(led1.devid); /* 获取分配号的主设备号 */
	led1.major = MINOR(led1.devid); /* 获取分配号的次设备号 */
	cdev_init(&led1.cdev, &yc_leds_fops);
	led1.cdev.owner = THIS_MODULE;
	cdev_add(&led1.cdev, led1.devid, 1);
	led1.class = class_create(THIS_MODULE, "yc-leds");
	led1.device = device_create(led1.class, NULL, led1.devid, NULL, "leds"); // /dev/leds
	if (IS_ERR(led1.device)) {
		return PTR_ERR(led1.device);
	}
	ret = device_create_file(led1.device, &yc_led_on_off_attr);
	if (ret < 0) {
		printk("device_create_file %s fail\n", yc_led_on_off_attr.attr.name);
		return ret;
	}
	printk(KERN_INFO "yc leds initialized\n");
	return 0;
}
static int led_remove(struct platform_device* dev)
{
	/*根据  资源*/
	cdev_del(&led1.cdev);
	unregister_chrdev_region(led1.devid, 1);
	device_destroy(led1.class, led1.devid);
	class_destroy(led1.class);

	printk(KERN_INFO "leds_exit exit\n");

	/*卸载字符设备驱动*/
	return 0;
}

static const struct of_device_id yc_led[] = {
	{
	    .compatible = "gpio-leds",
	},
	{},
};

/* 分配/设置/注册一个 platform_dirve */
static struct platform_driver led_drv = {
	.probe = led_probe,
	.remove = led_remove,
	.driver = {
	    .name = "gpio-leds",
	    .owner = THIS_MODULE,
	    .of_match_table = yc_led },
};

static int __init led_init(void)
{
	printk("[%s][%d]\n", __func__, __LINE__);
	platform_driver_register(&led_drv);
	return 0;
}

static void __exit led_exit(void)
{
	printk("[%s][%d]\n", __func__, __LINE__);
	platform_driver_unregister(&led_drv);
}
module_init(led_init);
module_exit(led_exit);

/* 描述驱动程序的一些信息，不是必须的 */
MODULE_AUTHOR("ChenBlue");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("YCKJ leds Driver");
MODULE_LICENSE("GPL");
